package info.sean.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IdNoUtil {

	public static String getGenderFromIdNo(String idNo) {

		String man = "男人";
		String female = "女人";
		String UKN ="未知";
		try {
			if (StringUtils.isEmpty(idNo)) {
				return UKN;
			}
			if (!idNo.toUpperCase().matches("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)")) {
				return UKN;
			}
			if (idNo.length() == 15) {
				String genderChar = idNo.substring(idNo.length() - 1);
				int gender = Integer.parseInt(genderChar) % 2;
				if (gender == 1) {
					return man;
				} else if (gender == 0) {
					return female;
				}
			} else if (idNo.length() == 18) {
				String genderChar = idNo.substring(idNo.length() - 2, idNo.length() - 1);
				int gender = Integer.parseInt(genderChar) % 2;
				if (gender == 1) {
					return  man;
				} else if (gender == 0) {
					return female;
				}
			}
		} catch (Exception e) {
			return UKN;
		}
		return UKN;
	}

	public static Date getBirthdayFromIdNo(String idNo) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try {
			return format.parse(idNo.substring(6, 14));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
